home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / ir / lock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-13  |  6.3 KB  |  260 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE:
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.    
  4.   
  5. */
  6.  
  7. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  8.  
  9.  
  10. #ifndef lint
  11. static char *RCSid = "$Header: /usr/local/ls6/src+data/src/freeWAIS-sf/ir/RCS/lock.c,v 1.4 1994/12/13 18:52:45 pfeifer Exp $";
  12. #endif
  13.  
  14. /* Change log:
  15.  * $Log: lock.c,v $
  16.  * Revision 1.4  1994/12/13  18:52:45  pfeifer
  17.  * chip@chinacat.unicom.com (Chip Rosenthal) patches.
  18.  * Excluding the merge of libinv and libwais
  19.  *
  20.  * Revision 1.3  1994/05/20  12:56:50  pfeifer
  21.  * beta
  22.  *
  23.  * Revision 1.2  1994/03/08  21:06:33  pfeifer
  24.  * Patchlevel 04
  25.  *
  26.  * Revision 1.1  1993/02/16  15:05:35  freewais
  27.  * Initial revision
  28.  *
  29.  * Revision 1.4  92/05/06  17:33:25  jonathan
  30.  * ?
  31.  * 
  32.  * Revision 1.3  92/04/28  17:05:23  jonathan
  33.  * K&R style definitions.
  34.  * 
  35.  */
  36.  
  37. /* ----------------------------------------------------------------------------
  38.  
  39.     COPYRIGHT    1992 Thinking Machines Corporation
  40.     AUTHOR        M. Tracy Shen
  41.                         Modified version of Gordon Linoff's lock.c
  42.     MODULE        lock.c -- handle lock files all in one place
  43.     INTERFACES
  44.             utlk_using_lock()   - check the existence of a lock file
  45.             utlk_set_lock()     - set a lock file if one doesn't exist
  46.             utlk_unset_lock()   - unset a lock file if one exists
  47.     INTERNAL ROUTINES
  48.     COMMENTS
  49. ---------------------------------------------------------------------------- */
  50.  
  51. #include "cdialect.h"
  52. #include <stdio.h> 
  53. #include <sys/param.h>
  54. #include <errno.h>        /* errno, EWOULDBLOCK, etc. */
  55.  
  56. #ifdef Mach
  57. extern int errno;
  58. #endif
  59.  
  60. #define SIGNOP 0        /* for using kill() to check if process is
  61.                    alive */ 
  62.  
  63. #define FALSE 0
  64. #define TRUE  1
  65.  
  66.  
  67. #define LOCK_STORAGE_MODULE
  68. #include "lock.h"
  69. #undef LOCK_STORSGE_MODULE
  70.  
  71. #ifndef MAXPATHLEN
  72. # define MAXPATHLEN 1024
  73. #endif
  74.  
  75. /******************************************************************************
  76.  *
  77.  *    Function Name:    utlk_using_lock_and_get_pid
  78.  *
  79.  *
  80.  *    Purpose:    check if update or query server is running
  81.  *
  82.  *    Returns:    return TRUE if process running, FALSE if not running
  83.  *            NOT_RUNNING.
  84.  *                      If pid is not NULL, sets it to the process id of
  85.  *                      locking process.
  86.  *
  87.  *    Algorithm:    Find a lock file in /tmp for update or query, read
  88.  *            pid from file, be sure process is running with that
  89.  *            pid.
  90.  *    End Algorithm
  91.  */
  92.  
  93. long
  94.     utlk_using_lock_and_get_pid (dbname, lock_type,pid_ptr)
  95. char *dbname;
  96. long    lock_type;
  97. unsigned *pid_ptr;
  98. {
  99.     /* local variables */
  100.     char lockfile_name[MAXPATHLEN];    /* absolute pathname of update lock */
  101.     FILE *file_ptr;            /* for opening lock files */
  102.     long pid;                /* pid of SC or update process */
  103.     long status;
  104.  
  105.     if (! IN_LOCK_RANGE(lock_type)) {
  106.     /* Out of range locks should never happen, but just in case . . . */
  107.     return(FALSE);
  108.         }
  109.  
  110.     sprintf( lockfile_name, "%s%s",dbname, lock_names[lock_type]);
  111.  
  112.     file_ptr = fopen(lockfile_name, "r"); /* look for an existing lock file */
  113.  
  114.     if (file_ptr == NULL) {     /* didn't find lock file! */
  115.     return(FALSE);
  116.        }
  117.  
  118.     /* read pid from the lock file */
  119.     status = fscanf(file_ptr, "%d", &pid);
  120.     fclose(file_ptr);
  121.     if (status == 0) {  /* is an empty lock file */
  122.     return(FALSE);
  123.         }
  124.  
  125.     /* Try a dummy kill; if ESRCH, process is not found.  Otherwise, assume
  126.      * it's there. */
  127.     status = kill(pid, SIGNOP);
  128.     if ((status != 0) && (errno == ESRCH)) {
  129.     return(FALSE);
  130.     }
  131.     if (pid_ptr != NULL) *pid_ptr = pid;
  132.     return(TRUE);
  133.  
  134.     }  /* end utlk_using_lock_and_get_pid */
  135.  
  136. /******************************************************************************
  137.  *
  138.  *    Function Name:    utlk_using_lock
  139.  *
  140.  *
  141.  *    Purpose:    check if update or query server is running
  142.  *
  143.  *    Returns:    return TRUE if process running, FALSE if not running
  144.  *            NOT_RUNNING.
  145.  *
  146.  *    Algorithm:    Find a lock file in /tmp for update or query, read
  147.  *            pid from file, be sure process is running with that
  148.  *            pid.
  149.  *    End Algorithm
  150.  */
  151.  
  152. long
  153.     utlk_using_lock (dbname, lock_type)
  154. char *dbname;
  155. long lock_type;
  156. {
  157.     return(utlk_using_lock_and_get_pid(dbname, lock_type, NULL));
  158. }  /* end utlk_using_lock */
  159.  
  160.  
  161.  
  162. /******************************************************************************
  163.  *
  164.  *    Function Name:    utlk_unset_lock
  165.  *
  166.  *
  167.  *    Purpose:    free lock
  168.  *
  169.  *    Returns:    Return TRUE is successful, FALSE if lock cannot be unset
  170.  *
  171.  *    Algorithm:    
  172.  *    End Algorithm
  173.  */
  174.  
  175. long
  176.     utlk_unset_lock (dbname, lock_type)
  177. char *dbname;
  178. long lock_type;
  179. {
  180.     /* local variables */
  181.     char lockfile_name[MAXPATHLEN];    /* absolute pathname of update lock */
  182.     
  183.     /* begin executable unset_lock */
  184.     
  185.     if (! IN_LOCK_RANGE(lock_type)) {
  186.     /* Out of range locks should never happen, but just in case . . . */
  187.     return(TRUE);
  188.     }
  189.     
  190.     sprintf( lockfile_name, "%s%s",dbname, lock_names[lock_type]);
  191.     
  192.     return(unlink(lockfile_name) == 0);
  193. }  /* end utlk_unset_lock */
  194.  
  195.  
  196.  
  197. /******************************************************************************
  198.  *
  199.  *    Function Name:    utlk_set_lock_with_pid
  200.  *
  201.  *
  202.  *    Purpose:    set lock, process id is argument
  203.  *
  204.  *    Returns:    Return TRUE is successful, FALSE if lock cannot be set
  205.  *
  206.  *    Algorithm:    
  207.  *    End Algorithm
  208.  */
  209.  
  210. long
  211.     utlk_set_lock_with_pid (dbname, lock_type, pid)
  212. char *dbname;
  213. long lock_type;            /* LOCK_UPDATE, etc. */
  214. long pid;            /* pid of SC or update process */
  215. {
  216.     /* local variables */
  217.     char lockfile_name[MAXPATHLEN];    /* absolute pathname of update lock */
  218.     FILE *file_ptr;            /* for opening lock files */
  219.  
  220.     /* begin executable set_lock */
  221.     
  222.     if (! IN_LOCK_RANGE(lock_type)) {
  223.     /* Out of range locks should never happen, but just in case . . . */
  224.     return(FALSE);
  225.     }
  226.     
  227.     sprintf( lockfile_name, "%s%s",dbname, lock_names[lock_type]);
  228.  
  229.     /* Don't look for an existing file -- just write over it */
  230.     file_ptr = fopen(lockfile_name, "w");
  231.     if (file_ptr == NULL) {    /* no write permission to db  */
  232.     return(FALSE);
  233.     }
  234.     fprintf(file_ptr, "%d", pid);
  235.     fclose(file_ptr);
  236.     return(TRUE);
  237. }  /* end utlk_set_lock */
  238.  
  239.  
  240. /******************************************************************************
  241.  *
  242.  *    Function Name:    utlk_set_lock
  243.  *
  244.  *
  245.  *    Purpose:    set lock
  246.  *
  247.  *    Returns:    Return TRUE is successful, FALSE if lock cannot be set
  248.  *
  249.  *    Algorithm:    
  250.  *    End Algorithm
  251.  */
  252.  
  253. long
  254.     utlk_set_lock (dbname, lock_type)
  255. char *dbname;
  256. long lock_type;
  257. {
  258.     return(utlk_set_lock_with_pid(dbname, lock_type, getpid()));
  259. }  /* end utlk_set_lock */
  260.